#!/usr/bin/python

# rss parser
# Author : James Tan
# Copyright: "(C) 20012-2013 James Tan. GNU GPL 3."
# Version: v0.1 (beta)

# History (update half a month)
# v0.1  - 01/15/2013  
#   basic functionalities

import feedparser
import optparse
import unicodedata

def getRssFeed(url, out):
  feed = feedparser.parse(url)
  if feed != "":
	for entry in feed.entries:
	  out.write('Title:' + unicodedata.normalize('NFKD',entry.title).encode('ascii','ignore') + '\n')
	  out.write('Link:' + unicodedata.normalize('NFKD',entry.link).encode('ascii','ignore') + '\n')
	  out.write('Time:' + unicodedata.normalize('NFKD',entry.published).encode('ascii','ignore') + '\n')
	  out.write('\n')

if __name__ == "__main__":
  p = optparse.OptionParser()
  p.add_option("-s", "--symbol", action="store", type="string", dest="symbol")
  (options, args) = p.parse_args()
  
  google_rss_base_url='http://www.google.com/finance/company_news?output=rss&q='
  yahoo_rss_base_url='http://feeds.finance.yahoo.com/rss/2.0/headline?region=US&lang=en-US&s='
  
  outFile = open('page.txt', 'w')
  outFile.write('News Feeds for ' + options.symbol + '\n')
  outFile.write('Google Feeds\n')
  getRssFeed(google_rss_base_url + options.symbol, outFile)
  outFile.write('Yahoo Feeds\n')
  getRssFeed(yahoo_rss_base_url + options.symbol, outFile)
  outFile.close()
